#include "../s21_decimal.h"

void s21_reset(s21_decimal *value) {
  for (int i = 0; i <= LAST; i++) {
    value->bits[i] = 0;
  }
}

int s21_has_long_bits(s21_long_decimal *value) {
  int result = FALSE;

  for (int i = LAST; i < LONG_LAST && result == FALSE; i++) {
    if (value->bits[i] != 0) {
      result = TRUE;
    }
  }

  return result;
}

int s21_is_not_max_value_ignore_scale(s21_long_decimal *value) {
  int result = FALSE;

  for (int i = 0; i < LAST && result == FALSE; i++) {
    if ((unsigned int)value->bits[i] < ALL_BIT) {
      result = TRUE;
    }
  }

  return result;
}

void s21_increase_long_by_one(s21_long_decimal *value) {
  for (int i = 0; i <= MAX_LONG_INDEX; i++) {
    if (s21_get_long_bit(value, i) == 1) {
      s21_reset_long_bit(value, i);
    } else {
      s21_set_long_bit(value, i);
      i = MAX_LONG_INDEX;  // break;
    }
  }
}

int s21_parse_int(char *string, char off_symbol, int *result) {
  int value = 0;
  int off_symbol_index = 0;

  for (int i = 0; string[i] != off_symbol && string[i] != '\0'; i++) {
    if (48 <= string[i] && string[i] <= 57) {
      value *= 10;
      value += (int)string[i] - 48;
    }

    off_symbol_index++;
  }

  if (string[0] == '-') {
    value *= -1;
  }

  *result = value;

  return off_symbol_index;
}